<?php


class page_admin {
	
	private $titre;
	private $contenu;
	private $page;
	private $url;
	private $logo;
	private $style=array('bootstrap', 'style-responsive', 'style');
	private $js=array();
	private $gritter;
	
	
	
	public function __construct($p){
		$this->page = $p;
		include_once('autoload.php');
		session_start();
	
	}
	
	public function URL($vfile)
	{
		$this->url = basename($vfile);
		
	}
	
	public function getUrl()
	{
		return $this->url;
	}
	
	public function __set($propriete, $valeur) {
		
		switch ($propriete) {
			case 'titre' : {
				$this->titre = $valeur;
				break;
			}
				
			case 'contenu' : {
				$this->contenu = $valeur;
				break;
			}
			
			case 'style' : {
				$this->style[count($this->style)+1] = $valeur;
				break;
			}
			
			case 'js' : {
				$this->js[count($this->js)+1] = $valeur;
				break;
			}
			
			case 'url' : {
				$this->url = $valeur;
				break;
			}
			
			case 'logo' : {
				$this->logo = $valeur;
				break;
			}
			
			case 'gritter' : {
				$this->gritter = $valeur;
				break;
			}
			
		}
	}
	
	/*Création d'un Objet "Preparation de requête" */
	public function PDO ()
	{
		require_once ('MyPDO.php');
		require_once 'PreparationREQPageAdmin.php';
		$PrepREQ = new PreparationREQPageAdmin(new MyPDO());
		return $PrepREQ;
	}
	
	/*************Gestion des styles *********************/
	private function affiche_style() {
		
		echo '<link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />';
		echo '<link rel="stylesheet" type="text/css" href="assets/js/gritter/css/jquery.gritter.css" />';
		foreach ($this->style as $s) {
			echo "<link rel='stylesheet' href='assets/css/".$s.".css' />\n";
		}
	}
	
	private function affiche_js() {
	
		foreach ($this->js as $s) {
			echo "<link rel='stylesheet' href='assets/js/".$s.".js' />\n";
		}
	}
	
	
	
	public function affiche_titre()
	{
		echo $this->titre;
	}
	
	public function trouverid()
	{
		$vretour;
		$PrepREQ = $this->PDO();
		$Result[] = $PrepREQ->TrouverID($this->titre, $this->url,$this->logo);
		$Count = 0;
		foreach ($Result as $v1) {	
    		$vretour = $v1['PAG_ID'];
    		$Count++;		
		}
		
		if ($Count > 1)
		{
			 return $Count.'pages avec ce titre, cette url et ce logo existent';
		}
		else
		{
			return $vretour;
		}
	}
	
	public function CreerNouvellePage()
	{
		
		$PrepREQ = $this->PDO();
		/* TODO test trouverid() n'est pas égal à une chaine de caractère*/
		if ($PrepREQ->selectById($this->trouverid()) == false)
		{
		$PrepREQ->insert($this->titre, $this->url, $this->logo);
		}
		else
		{
			return 'Une page similaire existe';
		}
		
		
	}
	public function affiche_nav()
	{
		$PrepREQ = $this->PDO();
		$row[] = $PrepREQ->SelectPage();
		foreach ($row as $v1) // Pour chaque résultats dans la requête on effectue :
		{
    		foreach ($v1 as $v2)  // Pour chaque 
			{
				if ($PrepREQ->TrouverSousPage($v2['PAG_ID']) != false)
				{
					$SP[] = $PrepREQ->TrouverSousPage($v2['PAG_ID']);
					
					if ($v2['PAG_ID'] == 1) {echo '<li class="mt sub-menu">';} else{ echo'<li class="sub-menu">';}
						
                          echo	'<a href="'.$v2['PAG_URL'].'" >	
                          		<i class="fa fa-'.$v2['PAG_LOGO'].'"></i>
	                        	<span>'.$v2['PAG_TITRE'].'</span>
	                       	 </a>	
	                        		<ul class="sub">';
                         	 			foreach ($SP as $ASP)
										{
                         	 				echo '<li><a  href='.$ASP['sp_url'].'>'.$ASP['sp_titre'].'</a></li>';
                         	 			}
                     				echo '</ul>
                         		</li>
                          	<li>';
                     }
                 
                 
                 else
                 {
                 	if ($v2['PAG_ID'] == 1) {echo '<li class="mt">';} else {echo '<li>';}
				echo '<a href="'.$v2['PAG_URL'].'" >
				<i class="fa fa-'.$v2['PAG_LOGO'].'"></i>
				<span>'.$v2['PAG_TITRE'].'</span></a>
				</li>';
                 }
				}
				
		}
	}
	    	
	public function affiche_contenu()
	{
		echo $this->contenu;
	}
	
	protected function affiche_connexion()
	{
		if(isset($_SESSION['enligne']))
		{
			$pseudo='';
			if(isset($_COOKIE['ASPCLogin'])){$pseudo=$_COOKIE['ASPCLogin'];}
			$this->affiche();
		}
	}
	
	private function deconnecte()
	{
		if(isset($_POST['deconnex'])){
			$_SESSION = array();
			session_destroy();
			$_SESSION['enligne'] = false;	
		}
	}
private function connecte()
	{
		if(isset($_POST['connex']))
		{
			if(isset($_POST['login']) && isset($_POST['mdp']))
			{
				if(!empty($_POST['login']) && !empty($_POST['mdp']))
				{
				
					$Admin = new administrateur($_POST['login'],$_POST['mdp']);
					$Profil = $Admin->Arrayprofil();
						if($Profil['ID'] == -1)
						{
							//echo'err id mp';
							$_SESSION['ERROR']=$Profil;
							
						}
						else
						{
							$_SESSION['enligne'] = true;
							$_SESSION['login']= $_POST['login'];
							$_SESSION['ID'] = $Profil['ID'];
							$_SESSION['Objet'] = serialize($Admin);
							setcookie('ASPCLogin', $_SESSION['login'], time() + 365*24*3600, null, null, false, true);
						}
					}
					
			}
		}
	}
	
	function affiche_photo_profil()
	{
		
		if (unserialize($_SESSION['Objet'])->Getphotoprofil()!= FALSE)
		{
			$vretour = unserialize($_SESSION['Objet'])->Getphotoprofil()['PHO_FICHIER'];
		}
		
		else
		{
			$vretour = "nophoto.jpg";	
		}
		
		return $vretour;
	}
	
	public function afficher_gritter()
	{
		if(isset ($this->gritter))
		{
		return '<script type="text/javascript">
		$(document).ready(function () {
			var unique_id = $.gritter.add({
				// (string | mandatory) the heading of the notification
				title: "Page Actualité Modification",
				// (string | mandatory) the text inside the notification
				text: "'.$this->gritter.'",
				// (string | optional) the image to display on the left
				image: $("#PhotoProfil").attr("src"),
				// (bool | optional) if you want it to fade out on its own or just sit there
				sticky: false,
				// (int | optional) the time you want it to be alive for before fading out
				time: "200",
				// (string | optional) the class name you want to apply to that specific message
				class_name: "my-sticky-class"
			});
		
				return false;
		});
		</script>';
		}
	}
	
	
	
	public function affiche()
	{
		
		$this->deconnecte();
		$this->connecte();
		if (isset($_SESSION['enligne']) && $_SESSION['enligne'] == true)
		{
		?><!DOCTYPE html>
		<html lang="fr">
		<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="icon" type="image/png" href="assets/img/LogoAspc1.png" />
		<?php $this->affiche_style(); ?>
		<?php $this->affiche_js();?>
		<title>
		<?php $this->affiche_titre(); ?>
		</title>
				
    		<script src="assets/js/chart-master/Chart.js"></script>
		    <script src="assets/js/ckeditor/ckeditor.js"></script>
		
		    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
		    <!--[if lt IE 9]>
		    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
		      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
		    </head>
		   <body>

 		 <section id="container" >
      <!-- **********************************************************************************************************************************************************
      TOP BAR CONTENT & NOTIFICATIONS
      *********************************************************************************************************************************************************** -->
     	 <!--header start-->
      	<header class="header black-bg">
              <div class="sidebar-toggle-box">
                  <div class="fa fa-bars tooltips" data-placement="right" data-original-title="Afficher menu"></div>
              </div>
            <!--logo start-->
            <a href="#" class="logo"><b>ASPC Badminton</b></a>
            <!--logo end-->
            <form method="post" action="#">
            <div class="top-menu">
            	<ul class="nav pull-right top-menu">
            	<input name="deconnex" type="hidden">
                    <li><button class="logout" type="submit">Déconnexion</a></li>
            	</ul>
            </div>
            </form>
        </header>
      <!--header end-->
      
      <!-- **********************************************************************************************************************************************************
      MAIN SIDEBAR MENU
      *********************************************************************************************************************************************************** -->
      <!--sidebar start-->
      <aside>
          <div id="sidebar"  class="nav-collapse ">
              <!-- sidebar menu start-->
              <ul class="sidebar-menu" id="nav-accordion">
              	  <p class="centered"><a href="profil.php"><img id="PhotoProfil" src="assets/img/PhotoProfil/<?php echo $this->affiche_photo_profil();?>" class="img-circle" width="120"></a></p>
	              	  <h5 class="centered"><?php echo utf8_encode((unserialize($_SESSION['Objet'])->ArrayProfil()['Prenom'].' '.unserialize($_SESSION['Objet'])->ArrayProfil()['Nom'])); ?></h5>  	
 					<?php $this->affiche_nav(); ?>
              </ul>
              <!-- sidebar menu end-->
          </div>
      </aside>
      <!--sidebar end-->
      
      <!-- **********************************************************************************************************************************************************
      MAIN CONTENT
      *********************************************************************************************************************************************************** -->
      <!--main content start-->
      <section id="main-content">
          <section class="wrapper">	

              <div class="row mt">
              
                    
			<?php $this->affiche_contenu(); ?>
              </div><!--row -->
          </section>
      </section>

      <!--main content end-->
      <!--footer start-->
      <footer class="site-footer">
          <div class="text-center">
              ASPC Badminton
              <a href="#" class="go-top">
                  <i class="fa fa-angle-up"></i>
              </a>
          </div>
      </footer>
      <!--footer end-->
  </section>

    <!-- js placed at the end of the document so the pages load faster -->
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/jquery-1.8.3.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script class="include" type="text/javascript" src="assets/js/jquery.dcjqaccordion.2.7.js"></script>
    
    <script src="assets/js/jquery.scrollTo.min.js"></script>
    <script src="assets/js/jquery.nicescroll.js" type="text/javascript"></script>
    <script src="assets/js/jquery.sparkline.js"></script>


    <!--common script for all pages-->
    <script src="assets/js/common-scripts.js"></script>
    
    <script type="text/javascript" src="assets/js/gritter/js/jquery.gritter.js"></script>
    <script type="text/javascript" src="assets/js/gritter-conf.js"></script>
	<script type='text/javascript' src='assets/js/jquery.cookie.js'></script>
	<script type='text/javascript' src='assets/js/jquery.hoverIntent.minified.js'></script>
	<script type="text/javascript" src="CLASS/Article/modifierarticle.js"></script>
	
	
    <!--script for this page-->
    <script src="assets/js/sparkline-chart.js"></script>    
	<script src="assets/js/zabuto_calendar.js"></script>
	
	
	<?php echo $this->afficher_gritter(); ?>
	
	<script type="application/javascript">
        $(document).ready(function () {
            $("#date-popover").popover({html: true, trigger: "manual"});
            $("#date-popover").hide();
            $("#date-popover").click(function (e) {
                $(this).hide();
            });
        
            $("#my-calendar").zabuto_calendar({
                action: function () {
                    return myDateFunction(this.id, false);
                },
                action_nav: function () {
                    return myNavFunction(this.id);
                },
                ajax: {
                    url: "show_data.php?action=1",
                    modal: true
                },
                legend: [
                    {type: "text", label: "Special event", badge: "00"},
                    {type: "block", label: "Regular event", }
                ]
            });
        });
        
        
        function myNavFunction(id) {
            $("#date-popover").hide();
            var nav = $("#" + id).data("navigation");
            var to = $("#" + id).data("to");
            console.log('nav ' + nav + ' to: ' + to.month + '/' + to.year);
        }
    </script>
  

  </body>
</html>
		    
<?php
		}
		else 
		{
		?>
		<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Dashboard">
    <meta name="keyword" content="Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina">

    <title>Identification</title>

    <!-- Bootstrap core CSS -->
    <link href="assets/css/bootstrap.css" rel="stylesheet">
    <!--external css-->
    <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
        
    <!-- Custom styles for this template -->
    <link href="assets/css/style.css" rel="stylesheet">
    <link href="assets/css/style-responsive.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
<?php print_r($_SESSION); print_r($_POST);?>
      <!-- **********************************************************************************************************************************************************
      MAIN CONTENT
      *********************************************************************************************************************************************************** -->

	  <div id="login-page">
	  	<div class="container">
	  	
		      <form class="form-login" method="post" action="#">
		        <h2 class="form-login-heading">Connectez-vous</h2>
		        <div class="login-wrap">
		            <input name="login" type="text" class="form-control" placeholder="Identifiant" autofocus>
		            <br>
		            <input name="mdp" type="password" class="form-control" placeholder="Mot de passe">
		            <input name="connex" type="hidden">
		            <label class="checkbox">
		                <span class="pull-right">
		                    <a data-toggle="modal" href="login.html#myModal">Mot de passe oublié ?</a>
		
		                </span>
		            </label>
		            <button class="btn btn-theme btn-block" href="#" type="submit"><i class="fa fa-lock"></i> Connexion</button>
		            <hr>
		            <!--<div class="registration">
		                Demandez un compte à l'administrateur<br/>
		                <a class="" href="#">
		                    Faite une demande
		                </a>
		            </div>
		
		        </div>
		
		          <!-- Modal --><!--
		          <div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
		              <div class="modal-dialog">
		                  <div class="modal-content">
		                      <div class="modal-header">
		                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		                          <h4 class="modal-title">Forgot Password ?</h4>
		                      </div>	
		                      <div class="modal-body"><!-- TODO OnClick Verifier adresse, si existe générer un nouveau MDP et envoi par mail entré  
		                          <p>Entrer votre adresse mail si dessous afin de réinitialiser votre Mot de passe</p>
		                          <input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">
		
		                      </div>
		                      <div class="modal-footer">
		                          <button data-dismiss="modal" class="btn btn-default" type="button">Annuler</button>
		                          <button class="btn btn-theme" type="button">Envoyer</button>
		                      </div>-->
		                  </div>
		              </div>
		          </div>
		          <!-- modal -->
		
		      </form>	  	
	  	
	  	</div>
	  </div>

    <!-- js placed at the end of the document so the pages load faster -->
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>

    <!--BACKSTRETCH-->
    <!-- You can use an image of whatever size. This script will stretch to fit in any screen size.-->
    <script type="text/javascript" src="assets/js/jquery.backstretch.min.js"></script>
    <script>
        $.backstretch("assets/img/login-bg.jpg", {speed: 500});
    </script>


  </body>
</html>
		<?php 
		}
	}

}